package com.briup.cms.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.bean.User;
import com.briup.cms.bean.extend.UserExtend;
import com.briup.cms.service.IUserService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author briup
 * @since 2025-03-19
 */
@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private IUserService userService;

/*
    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")  //@RequestAttribute 请参考JwtInterceptor中 38行代码
    public Result getInfo(@RequestAttribute("userId") Long id){
        log.info("id:{}",id);
        return Result.success(userService.queryById(id));
    }
*/

    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")
    public Result getInfo(HttpServletRequest request) {
        String jwt = request.getHeader("Authorization");
        log.info("jwt: {}", jwt);
        long id = JwtUtil.getUserId(jwt);
        log.info("id: {}", id);
        return Result.success(userService.queryById(id));
    }

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        userService.saveUser(user);
        return Result.success("新增成功");
    }
    @ApiOperation("根据Id查寻用户")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable Long id) {
        User u=userService.queryById(id);
        return Result.success(u);
    }
    @ApiOperation("根据Id分页查寻用户")
    @GetMapping("/query")
    public Result query( Integer pageNum,
                        Integer pageSize,
                         Integer isVip,
                         Integer roleId,
                         String status,
                         String username){
        IPage<UserExtend> u=userService.queryByPage(pageNum,pageSize,isVip,roleId,status,username);
        return Result.success(u);
    }
    @ApiOperation("更新用户vip")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {
        userService.setVip(id);
        return Result.success("vip更新成功");
    }
    @ApiOperation("更新用户")
    @PutMapping("/update")
    public Result setVip(@RequestBody User user){
        userService.update(user);
        return Result.success("更新成功");
    }
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteInBatch(@PathVariable("ids") List<Long> ids){
        log.info("ids: {}",ids);
        userService.deleteInBatch(ids);
        return Result.success("删除成功");
    }
}

